package com.bianguo.android.iocdemo.view.widget.parallax;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.bianguo.android.iocdemo.R;
import com.bianguo.android.iocdemo.adapter.ParallaxAdapter;
import com.bianguo.android.iocdemo.view.activity.ParallaxActivity;
import com.bianguo.android.iocdemo.view.fragment.ParallaxFragment;
import com.nineoldandroids.view.ViewHelper;

import java.util.ArrayList;

public class ParallaxPager extends FrameLayout implements ViewPager.OnPageChangeListener {

    private ViewPager mPager;
    private Context mContext;
    private ArrayList<ParallaxFragment> fragments;
    private ParallaxAdapter adapter;
    private int mViewPagerIndex;

    public ParallaxPager(@NonNull Context context) {
        this(context, null);
    }

    public ParallaxPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ParallaxPager(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context mContext, AttributeSet attrs) {
        this.mContext = mContext;
    }

    public void setUp(int... layoutIds) {
        fragments = new ArrayList<>();
        for (int layoutId : layoutIds) {
            ParallaxFragment fragment = ParallaxFragment.newInstance(layoutId);
            fragments.add(fragment);
        }
        mPager = new ViewPager(mContext);
        ParallaxActivity activity = (ParallaxActivity) getContext();
        mPager.setId(R.id.parallax_pager);
        mPager.addOnPageChangeListener(this);
        adapter = new ParallaxAdapter(activity.getSupportFragmentManager(), fragments);
        mPager.setAdapter(adapter);
        addView(mPager, 0);
    }

    /**
     * ViewPager在滑动过程中的回调
     * @param position 不管左滑还是右滑，都指向左边Pager的下标
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        int width = getWidth();
        ParallaxFragment outFragment = null;
        ParallaxFragment inFragment = null;
        if (mViewPagerIndex == position) {
            //左滑
            outFragment = fragments.get(position);
            if (position < fragments.size() - 1) {
                inFragment = fragments.get(position + 1);
            }
            if (outFragment != null) {
                ArrayList<View> views = outFragment.getParallaxView();
                if (views != null) {
                    for (View view : views) {
                        ParallaxViewTag tag = (ParallaxViewTag) view.getTag(R.id.parallax_tag);
                        if (tag == null) {
                            continue;
                        }
                        ViewHelper.setTranslationX(view, (0 - positionOffsetPixels) * tag.getxOut());
                        ViewHelper.setTranslationY(view, (0 - positionOffsetPixels) * tag.getyOut());
                    }
                }
            }

            if (inFragment != null) {
                ArrayList<View> views = inFragment.getParallaxView();
                if (views != null) {
                    for (View view : views) {
                        ParallaxViewTag tag = (ParallaxViewTag) view.getTag(R.id.parallax_tag);
                        if (tag == null) {
                            continue;
                        }
                        ViewHelper.setTranslationX(view, (width - positionOffsetPixels) * tag.getxOut());
                        ViewHelper.setTranslationY(view, (width - positionOffsetPixels) * tag.getyOut());
                    }
                }
            }
        } else {
            //右滑
            if (position < fragments.size() - 1) {
                outFragment = fragments.get(position + 1);
            }
            inFragment = fragments.get(position);
            if (outFragment != null) {
                ArrayList<View> views = outFragment.getParallaxView();
                if (views != null) {
                    for (View view : views) {
                        ParallaxViewTag tag = (ParallaxViewTag) view.getTag(R.id.parallax_tag);
                        if (tag == null) {
                            continue;
                        }
                        ViewHelper.setTranslationX(view, (width - positionOffsetPixels) * tag.getxOut());
                        ViewHelper.setTranslationY(view, (width - positionOffsetPixels) * tag.getyOut());
                    }
                }
            }

            if (inFragment != null) {
                ArrayList<View> views = inFragment.getParallaxView();
                if (views != null) {
                    for (View view : views) {
                        ParallaxViewTag tag = (ParallaxViewTag) view.getTag(R.id.parallax_tag);
                        if (tag == null) {
                            continue;
                        }
                        ViewHelper.setTranslationX(view, (0 - positionOffsetPixels) * tag.getxOut());
                        ViewHelper.setTranslationY(view, (0 - positionOffsetPixels) * tag.getyOut());
                    }
                }
            }
        }
    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        //state有三种状态下文会将，当手指刚触碰屏幕时state的值为1，我们就在这个时候给mViewPagerIndex 赋值。
        if (state == 1) {
            mViewPagerIndex = mPager.getCurrentItem();
        }
    }
}
